package com.yanglei.client;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.yanglei.util.Random;

public class singleClient {

	private JFrame jframe;
	private JPanel jpanel1;
	private JPanel jpanel2;
	private JPanel jpanel3;
	private JTextArea jtextarea;
	private JTextField jtextfield;
	private JButton jbutton;
	private JButton jbutton2;
	private JButton jbutton3;
	private String username2;// 聊天对象
	private ClientConnection clientConnection;
	private Client client;
	private String fileName;
	private String filePath;

	public ClientConnection getClientConnection() {
		return clientConnection;
	}

	public String getUsername2() {
		return username2;
	}

	public String getFileName() {
		return fileName;
	}

	public String getFilePath() {
		return filePath;
	}

	public JFrame getJframe() {
		return jframe;
	}

	public JTextArea getJtextarea() {
		return jtextarea;
	}

	public JTextField getJtextfield() {
		return jtextfield;
	}

	public singleClient(ClientConnection clientConnection, String username2) {
		this.clientConnection = clientConnection;
		this.username2 = username2;
		init();
	}

	private void init() {
		BorderLayout borderlayout = new BorderLayout();
		FlowLayout flowlayout = new FlowLayout();

		jframe = new JFrame("与" + username2 + "通话中");
		jpanel1 = new JPanel();
		jpanel2 = new JPanel();
		jpanel3 = new JPanel();
		jtextarea = new JTextArea(15, 35);
		jtextfield = new JTextField(20);
		jbutton = new JButton("发送");
		jbutton2 = new JButton("选择文件");
		jbutton3 = new JButton("发送文件");

		jbutton3.setEnabled(false);
		jbutton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				sendMessage();
			}
		});

		// 单击选择文件的同时将发送文件的按钮弹出
		jbutton2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				chooseFile();
				jbutton3.setEnabled(true);
			}
		});

		jbutton3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				sendFileMessage();

			}
		});

		this.jframe.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				try {
					// 发送窗口关闭信息
					clientConnection.sendSingleMessage("single client close",
							"13", username2);
					// 关闭私聊窗口的同时弹出群聊界面的私聊按钮
                  clientConnection.getClientMap().clear(); 
				} catch (Exception e2) {
					// TODO: handle exception
				} finally {

				}
			}
		});

		jtextarea.setEditable(false);

		jpanel1.setLayout(flowlayout);

		jpanel1.add(jtextfield);
		jpanel1.add(jbutton);
		jpanel1.add(jbutton2);
		jpanel1.add(jbutton3);
		jpanel1.setBorder(BorderFactory.createTitledBorder("发送消息"));

		jpanel3.add(jtextarea);
		jpanel3.setBorder(BorderFactory.createTitledBorder("私聊信息"));

		jpanel2.setLayout(borderlayout);

		jpanel2.add(jpanel1, borderlayout.SOUTH);
		jpanel2.add(jpanel3, borderlayout.CENTER);

		jframe.add(jpanel2);
		jframe.setResizable(false);
		jframe.setLocation(500, 250);
		jframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		jframe.setVisible(true);
		jframe.pack();

	}

	public void sendMessage() {
		String content = jtextfield.getText();

		jtextfield.setText("");

		jtextfield.requestFocus();

		clientConnection.sendSingleMessage(content, "9", username2);
	}

	public void chooseFile() {
		JFileChooser file = new JFileChooser();
		int resule = file.showOpenDialog(new JPanel());
		if (resule == file.APPROVE_OPTION) {
			this.fileName = file.getSelectedFile().getName();
			String dir = file.getSelectedFile().getPath();
			int t = JOptionPane.showConfirmDialog(null, dir, "选择的文件",
					JOptionPane.YES_OPTION);
			if (t == 0) {
				this.filePath = dir.replace('\\', '/');
				this.jtextfield.setText(filePath);
			}
		}
	}

	public void sendFileMessage() {
		String content = this.fileName;

		jtextfield.setText("");

		jtextfield.requestFocus();

		clientConnection.sendSingleMessage(content, "15", username2);
	}

}
